package com.drcurd.controller;

import com.drcurd.entity.Employee;
import com.drcurd.dto.EmployeeQueryDTO;
import com.drcurd.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/employees")
@CrossOrigin
public class EmployeeController {
    
    @Autowired
    private EmployeeService employeeService;
    
    // 查询所有员工
    @GetMapping
    public Map<String, Object> getAllEmployees() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Employee> employees = employeeService.getAllEmployees();
            result.put("success", true);
            result.put("data", employees);
            result.put("message", "查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }
    
    // 根据ID查询员工
    @GetMapping("/{id}")
    public Map<String, Object> getEmployeeById(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            Employee employee = employeeService.getEmployeeById(id);
            if (employee != null) {
                result.put("success", true);
                result.put("data", employee);
                result.put("message", "查询成功");
            } else {
                result.put("success", false);
                result.put("message", "员工不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }
    
    // 新增员工
    @PostMapping
    public Map<String, Object> addEmployee(@RequestBody Employee employee) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = employeeService.addEmployee(employee);
            if (success) {
                result.put("success", true);
                result.put("message", "新增成功");
            } else {
                result.put("success", false);
                result.put("message", "新增失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "新增失败: " + e.getMessage());
        }
        return result;
    }
    
    // 更新员工
    @PutMapping("/{id}")
    public Map<String, Object> updateEmployee(@PathVariable Long id, @RequestBody Employee employee) {
        Map<String, Object> result = new HashMap<>();
        try {
            employee.setId(id);
            boolean success = employeeService.updateEmployee(employee);
            if (success) {
                result.put("success", true);
                result.put("message", "更新成功");
            } else {
                result.put("success", false);
                result.put("message", "更新失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新失败: " + e.getMessage());
        }
        return result;
    }
    
    // 删除员工
    @DeleteMapping("/{id}")
    public Map<String, Object> deleteEmployee(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = employeeService.deleteEmployee(id);
            if (success) {
                result.put("success", true);
                result.put("message", "删除成功");
            } else {
                result.put("success", false);
                result.put("message", "删除失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "删除失败: " + e.getMessage());
        }
        return result;
    }
    
    // 条件查询员工
    @GetMapping("/search")
    public Map<String, Object> searchEmployees(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String email,
            @RequestParam(required = false) Long deptId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Map<String, Object> result = new HashMap<>();
        try {
            EmployeeQueryDTO queryDTO = new EmployeeQueryDTO();
            queryDTO.setName(name);
            queryDTO.setEmail(email);
            queryDTO.setDeptId(deptId);
            queryDTO.setOffset((page - 1) * size);
            queryDTO.setSize(size);
            
            List<Employee> employees = employeeService.getEmployeesByCondition(queryDTO);
            int total = employeeService.getEmployeeCountByCondition(queryDTO);
            
            result.put("success", true);
            result.put("data", employees);
            result.put("total", total);
            result.put("page", page);
            result.put("size", size);
            result.put("message", "查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }
    
    // 分页查询
    @GetMapping("/page")
    public Map<String, Object> getEmployeesByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<Employee> employees = employeeService.getEmployeesByPage(page, size);
            int total = employeeService.getEmployeeCount();
            result.put("success", true);
            result.put("data", employees);
            result.put("total", total);
            result.put("page", page);
            result.put("size", size);
            result.put("message", "查询成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        return result;
    }
} 